﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceZero.Business.Models;
using IceZero.DataStore;
using IceZero.DataStore.Models;
using NHibernate.Criterion;

namespace IceZero.Business
{
    /// <summary>
    /// 
    /// </summary>
    public class TableRecordIncrementBusiness
    {
        private static object _getTableFieldNoLock = new();
        public TableRecordIncrementDataStore TableRecordIncrementDataStore { get; set; }
        public TableRecordIncrementBusiness(TableRecordIncrementDataStore tableRecordIncrementDataStore)
        {
            TableRecordIncrementDataStore = tableRecordIncrementDataStore;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <param name="orderNumber"></param>
        /// <param name="stepNumber"></param>
        public void AddTableFieldNo(string tableName, string columnName, int orderNumber = 0, int stepNumber = 1)
        {
            var item = TableRecordIncrementDataStore.QueryOne(expressions =>
            {
                expressions.Add(Restrictions.Eq("TableName", tableName));
                expressions.Add(Restrictions.Eq("ColumnName", columnName));
                return expressions;
            });
            if (item != null)
            {
                throw new IceZeroBusinessException("记录已存在");
            }
            TableRecordIncrementDataStore.Save(new TableRecordIncrement()
            {
                TableName = tableName,
                ColumnName = columnName,
                OrderNumber = orderNumber,
                StepNumber = stepNumber
            });
            //if (_addTableFieldNoLock != null)
            //{
            //    lock (_addTableFieldNoLock)
            //    {
            //        if (_addTableFieldNoLock != null)
            //        {

            //        }
            //    }
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        public int GetTableFieldNo(string tableName, string columnName)
        {
            /*
            if (_getTableFieldNoFlag == null)
            {
                lock (_getTableFieldNoLock)
                {
                    if (_getTableFieldNoFlag == null)
                    {
                        _getTableFieldNoFlag = new();
                        var item = TableRecordIncrementDataStore.QueryOne(expressions =>
                        {
                            expressions.Add(Restrictions.Eq("TableName", tableName));
                            expressions.Add(Restrictions.Eq("ColumnName", columnName));
                            return expressions;
                        });
                        if (item == null)
                        {
                            throw new IceZeroBusinessException("记录不存在");
                        }
                        item.OrderNumber += item.StepNumber;
                        item.UpdateTime = DateTime.Now;
                        TableRecordIncrementDataStore.Update(item);
                        _getTableFieldNoFlag = null;
                        return item.OrderNumber;
                    }
                }
            }
            return -1;
            */
            lock (_getTableFieldNoLock)
            {
                var item = TableRecordIncrementDataStore.QueryOne(expressions =>
                {
                    expressions.Add(Restrictions.Eq("TableName", tableName));
                    expressions.Add(Restrictions.Eq("ColumnName", columnName));
                    return expressions;
                });
                if (item == null)
                {
                    throw new IceZeroBusinessException("记录不存在");
                }
                item.OrderNumber += item.StepNumber;
                item.UpdateTime = DateTime.Now;
                TableRecordIncrementDataStore.Update(item);
                return item.OrderNumber;
            }
        }
    }
}
